package com.mlh.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class MyInterceptor01 implements HandlerInterceptor {
	//1. preHandle() 在目标方法执行前被执行
	//2.如果preHandle() 返回false ，不再执行目标方法 postHandle和afterCompletion方法也不会被执行
	//3.该方法可以获取到request, response, handler
	//4.这里根据业务，可以进行拦裁，并指定跳转到哪个页面
	@Override
	public boolean preHandle(HttpServletRequest request,
							 HttpServletResponse response,
							 Object handler) throws Exception {
		System.out.println("MyInterceptor01-->preHandle()被执行");
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
						   HttpServletResponse response,
						   Object handler,
						   ModelAndView modelAndView) throws Exception {
		System.out.println("MyInterceptor01-->postHandle()被执行");
	}

	//在网页端 当afterCompletion方法都执行完了 网页端才会收到回复
	@Override
	public void afterCompletion(HttpServletRequest request,
								HttpServletResponse response,
								Object handler,
								Exception ex) throws Exception {
		System.out.println("MyInterceptor01-->afterCompletion()被执行");
	}
}
